package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import service.MovieService;
import service.UserService;

/**
 * Servlet implementation class PurchaseServlet
 */
@WebServlet("/PurchaseServlet")
public class PurchaseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PurchaseServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("gbk");
		UserService us = new UserService();
		HttpSession ss = request.getSession();
		String id = ss.getAttribute("id").toString();
		String realpassword = us.find(id);
		
		String password = request.getParameter("password");
		if(!password.equals(realpassword)){
			PrintWriter out = response.getWriter();
			//out.println("<%@ page language="java" import='java.util.*' pageEncoding='utf-8' %> ");
			out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
			out.println("<HTML>");
			out.println("  <HEAD><TITLE>登录失败</TITLE></HEAD>");
			out.println("  <BODY>");
            out.println("<h1>帐号或用户名错误!</h1>");
            out.println(" <a href='movie.jsp'>点击返回</a>");
			out.println("  </BODY>");
			out.println("</HTML>");
			out.flush();
			out.close();
		}
		else{
			MovieService ms = new MovieService();
			ms.purchase((request.getParameter("mid")), id);
			response.sendRedirect("inform.jsp");
		}
		
	}

}
